"""
The application's Globals object
"""

"""
    This file is part of project webCA

    webCA is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    webCA is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with webCA. If not, see <http://www.gnu.org/licenses/>.
"""

import parser, os
from pylons import config

class Globals(object):

    """Globals acts as a container for objects available throughout the
    life of the application

    """

    def __init__(self):
        """One instance of Globals is created during application
        initialization and is available during requests via the
        'app_globals' variable

        """
        self.ca_version_string          = 'webCA Version 0.1'
                
        # check if the openssl file is really there
        if os.path.exists(config['openssl_config']):
            # lets parse the file into a dict
            self.openssl_config         = parser.get_all_sections(config['openssl_config'])
            self.default_ca             = self.openssl_config['ca']['default_ca'] 
            self.ca_pass                = config['ca_pass']
            self.ca_debug               = config['ca_debug']
            self.openssl_bin            = config['openssl_bin']
            # lets try to fill our vars 
            try:            
                self.dir                = self.openssl_config[self.default_ca]['dir'] 
                self.certs              = self.openssl_config[self.default_ca]['certs']
                self.crl_dir            = self.openssl_config[self.default_ca]['crl_dir']
                self.database           = self.openssl_config[self.default_ca]['database']
                self.new_certs_dir      = self.openssl_config[self.default_ca]['new_certs_dir']
                self.certificate        = self.openssl_config[self.default_ca]['certificate']
                self.serial             = self.openssl_config[self.default_ca]['serial']
                self.crlnumber          = self.openssl_config[self.default_ca]['crlnumber']
                self.crl                = self.openssl_config[self.default_ca]['crl']
                self.private_key        = self.openssl_config[self.default_ca]['private_key']
                self.RANDFILE           = self.openssl_config[self.default_ca]['RANDFILE']
                self.x509_extensions    = self.openssl_config[self.default_ca]['x509_extensions']
                self.name_opt           = self.openssl_config[self.default_ca]['name_opt']
                self.cert_opt           = self.openssl_config[self.default_ca]['cert_opt']
                self.default_days       = self.openssl_config[self.default_ca]['default_days']
                self.default_crl_days   = self.openssl_config[self.default_ca]['default_crl_days']
                self.default_md         = self.openssl_config[self.default_ca]['default_md']
                self.preserve           = self.openssl_config[self.default_ca]['preserve']
                self.policy             = self.openssl_config[self.default_ca]['policy']
                self.key_dir            = self.openssl_config[self.default_ca]['dir'] + '/private'
                self.csr_dir            = self.openssl_config[self.default_ca]['dir'] + '/csr'
            except KeyError, ke:
                print 'error in Globals.__init__(): key', ke, 'does not exist in:', config['openssl_config']
        else:
            print 'cannot find:', config['openssl_config']
            exit()
                
                

